package org.benxiong.iplay.ui.component;

import org.benxiong.iplay.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * @author Seya
 * 
 */
public class ConversationView extends ScrollView {
	private LayoutInflater	mInflater;
	private LinearLayout	contentLayout;

	public ConversationView(Context context) {
		super(context);
		mInflater = LayoutInflater.from(context);
		contentLayout = new LinearLayout(context);
		contentLayout.setOrientation(LinearLayout.VERTICAL);
		this.addView(contentLayout);
	}

	public ConversationView(Context context, AttributeSet set) {
		super(context, set);
		mInflater = LayoutInflater.from(context);
		contentLayout = new LinearLayout(context);
		contentLayout.setOrientation(LinearLayout.VERTICAL);
		this.addView(contentLayout);
	}

	public void addMessage(Bitmap bitmap, int position) {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(5, 10, 10, 5);
		
		View view = null;
		if (position == 0) {
			view = mInflater.inflate(R.layout.left_msg, null);
		} else if (position == 1) {
			view = mInflater.inflate(R.layout.right_msg, null);
		} else {
			throw new UnknownError("Unknow position");
		}
		
		ImageView imgV = (ImageView) view.findViewById(R.id.avator);
		TextView tv = (TextView) view.findViewById(R.id.msg);
		tv.setVisibility(View.GONE);
		ImageView photoV = (ImageView) view.findViewById(R.id.photo);
		photoV.setImageBitmap(bitmap);
		photoV.setVisibility(View.VISIBLE);
		
		contentLayout.addView(view, lp);
		
		scrollToBottom();
	}
	/**
	 * 
	 * @param msg
	 *            - text messsage
	 * @param position
	 *            -0 for left, 1 for right
	 */
	public void addMessage(String msg, int position) {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(5, 10, 10, 5);
		
		View view = null;
		if (position == 0) {
			view = mInflater.inflate(R.layout.left_msg, null);
		} else if (position == 1) {
			view = mInflater.inflate(R.layout.right_msg, null);
		} else {
			throw new UnknownError("Unknow position");
		}
		
		ImageView imgV = (ImageView) view.findViewById(R.id.avator);
		TextView tv = (TextView) view.findViewById(R.id.msg);
		tv.setText(msg);
		
		contentLayout.addView(view, lp);
		
		scrollToBottom();
	}

	public void scrollToBottom() {
		final View scroll = this; 
		final View inner = contentLayout;
		
		Handler mHandler = new Handler();

		mHandler.post(new Runnable() {
			public void run() {
				if (scroll == null || inner == null) {
					return;
				}

				int offset = inner.getMeasuredHeight() - scroll.getHeight();
				if (offset < 0) {
					offset = 0;
				}

				scroll.scrollTo(0, offset);
			}
		});
	}
}
